﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Specialized;
using System.Text;
using Microsoft.SharePoint.Utilities;

namespace MII_FIF.Layouts.MII_FIF.FormApproval
{
    public partial class Approval_CRStorage : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Generic.isApprover())
            //{
            //    btnCancel_Click(sender, e);
            //}
            //((Label)Header.FindControl("lblHeader")).Text = "Form Approval Change Request Storage";
            btnApprove.Click += btnApprove_Click;
            btnReject.Click += btnReject_Click;
            btnCancel.Click += btnCancel_Click;
            if (!IsPostBack)
            {
                load();
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Task.aspx"));
        }

        void btnReject_Click(object sender, EventArgs e)
        {
            lblerror.Text = string.Empty;
            if (validasi())
            {
                InventoryManagement.Approval apps = new InventoryManagement.Approval();
                apps.ID = Guid.Parse(ViewState["ID"].ToString());
                apps.Select();

                InventoryManagement.CR_Storage CR = new InventoryManagement.CR_Storage();
                CR.ID = apps.IDChild;
                CR.Select();

                if (apps.Approver != null)
                {
                    lblerror.Text += "This Request already Approved/Rejected, please refresh your browser. <br />";
                }
                else
                {
                    CR.wfStatus = word.Rejected;
                    apps.Action = word.Reject;
                    apps.Approver = Generic.GetUserLoginReformat();
                    apps.ApprovalDate = DateTime.Now;
                    apps.Comment = txtComment.Text;
                    apps.Update();
                    CR.Update();

                    //#region sendemail
                    //#region headerEmail
                    //StringDictionary headers = new StringDictionary();
                    //headers.Add("to", Generic.getEmailUser(apps.Requester));
                    //headers.Add("cc", "");
                    //headers.Add("bcc", "");
                    //headers.Add("from", "");
                    //headers.Add("subject", "Request Rejected");
                    //headers.Add("content-type", "text/html");
                    //#endregion
                    //#region bodyEmail
                    StringBuilder sb = new StringBuilder();
                    sb.Append("Request Change Request Storage Rejected By " + SPContext.Current.Web.CurrentUser.Name);
                    Generic.sendEmailSMTP("System@fif.co.id", Generic.getEmailUser(apps.Requester), "Request Rejected", sb.ToString());
                    //#endregion
                    //SPUtility.SendEmail(SPContext.Current.Web, headers, sb.ToString());
                    //#endregion

                    Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Task.aspx"));
                }
            }
        }

        void btnApprove_Click(object sender, EventArgs e)
        {
            lblerror.Text = string.Empty;
            if (validasi())
            {
                InventoryManagement.Approval apps = new InventoryManagement.Approval();
                apps.ID = Guid.Parse(ViewState["ID"].ToString());
                apps.Select();

                InventoryManagement.CR_Storage CR = new InventoryManagement.CR_Storage();
                CR.ID = apps.IDChild;
                CR.Select();

                if (apps.Approver != null)
                {
                    lblerror.Text += "This Request already Approved/Rejected, please refresh your browser. <br />";
                }
                else
                {
                    CR.wfStatus = word.Approved;
                    apps.Action = word.Approve;
                    apps.Approver = Generic.GetUserLoginReformat();
                    apps.ApprovalDate = DateTime.Now;
                    apps.Comment = txtComment.Text;
                    apps.Update();
                    CR.Update();
                    Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Task.aspx"));
                }
            }
        }

        void load()
        {
            if (Request.QueryString["ID"] != null)
            {
                ViewState["ID"] = Request.QueryString["ID"].ToString();

                DataTable DTCR = new DataTable();
                DTCR.Columns.Add("Isi", typeof(string));

                DataRow DR = DTCR.NewRow();
                DR["Isi"] = string.Empty;
                DTCR.Rows.Add(DR);
                ViewState["DTCR"] = DTCR;
                GridChange.DataSource = DTCR;
                GridChange.DataBind();

                InventoryManagement.CR_Storage CR = new InventoryManagement.CR_Storage();
                InventoryManagement.Approval apps = new InventoryManagement.Approval();
                InventoryManagement.Inventory_Storage storage = new InventoryManagement.Inventory_Storage();
                apps.ID = Guid.Parse(ViewState["ID"].ToString());
                apps.Select();

                CR.ID = apps.IDChild;
                CR.Select();

                if (CR.wfStatus.ToLower() == word.Approve.ToLower() || CR.wfStatus.ToLower() == word.Reject || CR.wfStatus.ToLower() == word.Rejected.ToLower() || CR.wfStatus.ToLower() == word.Approved)
                {
                    btnApprove.Visible = false;
                    btnReject.Visible = false;
                }

                storage.ID = CR.ID_Storage;
                storage.Select();

                lblDate.Text = CR.Date.Value.ToString("dd MMM yyyy");
                lblHostName.Text = CR.HostName;
                lblInventoryBy.Text = CR.InventoryBy;
                lblIP.Text = CR.IP;
                lblLabelName.Text = CR.LabelName;
                lblRackNumber.Text = CR.RackNumber;
                chkInterface.Checked = CR.CR_Interface.Value;
                chkLogicalConnection.Checked = CR.CR_LogicalConnection.Value;
                chkSystem.Checked = CR.CR_System.Value;
                chkVLAN.Checked = CR.CR_VLAN.Value;
                ((TextBox)GridChange.Rows[0].FindControl("txtIsi")).Text = CR.Configuration;
                ViewState["Link"] = CR.URLAttachment;
                if (ViewState["Link"] != null)
                    lblFileName.Text = CR.URLAttachment.Substring(CR.URLAttachment.LastIndexOf("/") + 1, CR.URLAttachment.Length - CR.URLAttachment.LastIndexOf("/") - 1);
                disable();
            }
        }

        bool validasi()
        {
            bool check = true;
            return check;
        }

        void disable()
        {
            chkInterface.Enabled = false;
            chkLogicalConnection.Enabled = false;
            chkSystem.Enabled = false;
            chkVLAN.Enabled = false;
            rbBladeServer.Enabled = false;
            rbMainframe.Enabled = false;
            rbRackMountServer.Enabled = false;
            rbStorage.Enabled = false;
            rbTapeStorage.Enabled = false;
            rbTowerServer.Enabled = false;
            rbVMWare.Enabled = false;
            ((TextBox)GridChange.Rows[0].FindControl("txtIsi")).Attributes.Add("readonly", "true");
            txtLocation.Attributes.Add("readonly", "true");
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (ViewState["Link"] != null)
            {
                string script =
                               "function download() {" +
                               "var link ='" + ViewState["Link"].ToString() + "';" +
                               "var retvalue = window.open('" + Generic.GenLink("/_layouts/MII_FIF/Download.aspx") + "?links=' + link"
                             + ", '', 'width=1000,height=1000');}";

                Page.ClientScript.RegisterClientScriptBlock(typeof(System.Web.UI.Page), "opendownload", script, true);
            }
        }
    }
}
